Grokking LinkedIn Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
LinkedIn Easy
Find Pivot Index (easy)
Solution: Find Pivot Index
Isomorphic Strings (easy)
Solution: Isomorphic Strings
Second Minimum Node In a Binary Tree (easy)
Solution: Second Minimum Node In a Binary Tree
Minimum Time to Type Word Using Special Typewriter (easy)
Solution: Minimum Time to Type Word Using Special Typewriter
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Can Place Flowers (easy)
Solution: Can Place Flowers
Valid Perfect Square (easy)
Solution: Valid Perfect Square
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Sqrt (easy)
Solution: Sqrt
Valid Palindrome (easy)
Solution: Valid Palindrome
Rotate List (medium)
Solution: Rotate List
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Valid Triangle Number (medium)
Solution: Valid Triangle Number
Valid Parenthesis String (medium)
Solution: Valid Parenthesis String
Edit Distance (medium)
Solution: Edit Distance
Remove All Ones With Row and Column Flips II (medium)
Solution: Remove All Ones With Row and Column Flips II
Reverse Integer (medium)
Solution: Reverse Integer
Triplet Sum to Zero (medium)
Solution: Triplet Sum to Zero
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Decode Ways (medium)
Solution: Decode Ways
Quadruple Sum to Target (medium)
Solution: Quadruple Sum to Target
Next Permutation (medium)
Solution: Next Permutation
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Word Search (medium)
Solution: Word Search
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
House Robber II (medium)
Solution: House Robber II
Coin Change
Solution: Coin Change
Design Add and Search Words Data Structure (medium)
Solution: Design Add and Search Words Data Structure
Merge Intervals (medium)
Solution: Merge Intervals
Number of Islands (easy)
Solution: Number of Islands
Longest Palindromic Subsequence (medium)
Solution: Longest Palindromic Subsequence
Kth Smallest Number (medium)
Solution: Kth Smallest Number
Maximum Product Subarray (medium)
Solution: Maximum Product Subarray
Same Tree (medium)
Solution: Same Tree
Max Points on a Line (hard)
Solution: Max Points on a Line
Closest Binary Search Tree Value II (hard)
Solution: Closest Binary Search Tree Value II
Word Ladder (hard)
Solution: Word Ladder
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Solution: Minimum Time to Type Word Using Special Typewriter

Problem Statement

You are given a circular keyboard that has all the lowercase English letters ('a' to 'z') laid out in a circle. You can type a particular character if the point is pointed to that character. Initially, a cursor points at the letter 'a'.

At each second, you can perform the following operation:

  • Move the cursor either one step to the left or right.
  • Type the letter where the pointer points currently.

Given a string word, return the minimum number of seconds to type out the characters in a word.

Examples

  • Example 1:

    • Input: "bad"
    • Expected Output: 8
    • Justification: Start at 'a'. Move to 'b' (1 second), type 'b' (1 second), move to 'a' (1 seconds), type 'a' (1 second), move to 'd' from 'a' (3 second), and type 'd' (1 second). Total = 8 seconds.
  • Example 2:

    • Input: "zigzag"
    • Expected Output: 32
    • Justification: Start at 'a'. Move to 'z' (1 second), type 'z' (1 second), move to 'i' (9 seconds, choosing the shorter path), type 'i' (1 second), move to 'g' (2 seconds), type 'g' (1 second), move to 'z' (7 seconds), type 'z' (1 second), move to 'a' (1 second), and type 'a' (1 second), move to 'g' (6 seconds), type 'g' (1 second). Total = 32 seconds.
  • Example 3:

    • Input: "ace"
    • Expected Output: 7
    • Justification: Start at 'a', type 'a' (1 second), move to 'c' (2 seconds), type 'c' (1 second), move to 'e' (2 seconds), and type 'e' (1 second). Total = 7 seconds.

Solution

To solve this problem, we adopt a strategy that minimizes the movement around the circular keyboard. For each letter in the target word, we calculate the minimum distance from the current letter to the target letter, considering both clockwise and counterclockwise movements.

This approach ensures that we always take the shortest path to the next letter, significantly reducing the total time taken to type the word. The time taken to type each letter is constant, so our primary focus is on minimizing cursor movement. This method is effective because it leverages the circular nature of the keyboard layout, ensuring that we exploit the shortest possible route to each letter, which is inherently the most efficient approach for this particular problem.

Step-by-Step Algorithm

  1. Initialize Variables:

    • totalTime to 0, which will hold the total time taken to type the word.
    • previousChar to 'a', representing the starting point of the typewriter cursor.
  2. Iterate Through Each Character of the Word:

    • For each character currentChar in the input word, perform the following steps:
  3. Calculate Distance:

    • Determine the ASCII value difference between currentChar and previousChar to find the direct distance distance between them.
  4. Calculate Steps:

    • Compute the clockwise and counterclockwise steps required to move from previousChar to currentChar. This is done by taking the minimum of distance and 26 - distance (total letters in the alphabet minus the direct distance), ensuring the shortest path is chosen.
  5. Update Total Time:

    • Add the calculated steps plus one (for typing the character) to totalTime. The "+1" accounts for the action of typing the current character.
  6. Update Previous Character:

    • Set previousChar to the current character currentChar to prepare for the next iteration.
  7. Return Total Time:

    • After iterating through all characters in the word, return totalTime as the total time taken to type the word.

Algorithm Walkthrough

Consider the input word "zigzag" for the algorithm walkthrough:

  • Initialize:

    • totalTime = 0
    • previousChar = 'a'
  • For 'z':

    • currentChar = 'z'
    • distance = abs('z' - 'a') = 25
    • steps = min(25, 26 - 25) = 1 (shortest path is 1 step)
    • totalTime += 1 (move) + 1 (type) = 2
    • previousChar = 'z'
  • For 'i':

    • currentChar = 'i'
    • distance = abs('i' - 'z') = 9
    • steps = min(9, 26 - 9) = 9
    • totalTime += 9 (move) + 1 (type) = 12
    • previousChar = 'i'
  • For 'g':

    • currentChar = 'g'
    • distance = abs('g' - 'i') = 2
    • steps = min(2, 26 - 2) = 2
    • totalTime += 2 (move) + 1 (type) = 15
    • previousChar = 'g'
  • For 'z' (again):

    • currentChar = 'z'
    • distance = abs('z' - 'g') = 7
    • steps = min(7, 26 - 7) = 7
    • totalTime += 7 (move) + 1 (type) = 23
    • previousChar = 'z'
  • For 'a':

    • currentChar = 'a'
    • distance = abs('a' - 'z') = 1 (since 'z' to 'a' is a direct step)
    • steps = min(1, 26 - 1) = 1
    • totalTime += 1 (move) + 1 (type) = 25
    • previousChar = 'a'
  • For 'g' (again):

    • currentChar = 'g'
    • distance = abs('g' - 'a') = 6
    • steps = min(6, 26 - 6) = 6
    • totalTime += 6 (move) + 1 (type) = 32
    • previousChar = 'g'
  • Final Total Time: 32 seconds

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

  • : The primary operation in the algorithm is iterating through each character of the input string, where (n) is the length of the string. For each character, we perform constant time operations, which do not depend on the size of the string. Thus, the overall time complexity is linear relative to the length of the input string.

Space Complexity

  • : The space complexity is constant because the amount of memory used does not scale with the size of the input.
Minimum Time to Type Word Using Special Typewriter (easy)
Intersection of Two Arrays (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity